home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / CocktailManager / CoctailManager.jar / App.class (.txt) next >
Encoding:
Java Class File  |  2001-09-07  |  10.5 KB  |  348 lines

  1. import com.siemens.mp.gsm.SMS;
  2. import java.io.IOException;
  3. import javax.microedition.lcdui.Alert;
  4. import javax.microedition.lcdui.Command;
  5. import javax.microedition.lcdui.CommandListener;
  6. import javax.microedition.lcdui.Display;
  7. import javax.microedition.lcdui.Displayable;
  8. import javax.microedition.lcdui.Form;
  9. import javax.microedition.lcdui.Image;
  10. import javax.microedition.lcdui.ImageItem;
  11. import javax.microedition.lcdui.List;
  12. import javax.microedition.lcdui.TextField;
  13. import javax.microedition.midlet.MIDlet;
  14.  
  15. public class App extends MIDlet implements CommandListener {
  16.    private Display display = Display.getDisplay(this);
  17.    private List menuScreen;
  18.    private List browseScreen;
  19.    private Form searchScreen;
  20.    private Form coctailScreen;
  21.    private Form newCoctailScreen;
  22.    private Form SMSScreen;
  23.    private Form splashScreen;
  24.    private Form helpScreen;
  25.    private Command menuCommand;
  26.    private Command quitCommand;
  27.    private Command exitCommand;
  28.    private Command backCommand;
  29.    private Command aboutCommand;
  30.    private Command helpCommand;
  31.    private Command searchCommand;
  32.    private Command newCommand;
  33.    private Command score1Command;
  34.    private Command score2Command;
  35.    private Command score3Command;
  36.    private Command score4Command;
  37.    private Command score5Command;
  38.    private Command sendSMSCommand;
  39.    private CoctailBar coctailBar;
  40.    private TextField phoneNumberField;
  41.    private TextField searchByNameField;
  42.    private TextField searchByIngredientsField;
  43.    private TextField coctailNameField;
  44.    private TextField coctailIngredientsField;
  45.    private TextField coctailRecipeField;
  46.  
  47.    private void showSplash() {
  48.       Image splashImage = null;
  49.  
  50.       try {
  51.          splashImage = Image.createImage("/splash.png");
  52.       } catch (IOException var3) {
  53.       }
  54.  
  55.       this.splashScreen = new Form(CoctailDatabase.getString(0));
  56.       ImageItem it = new ImageItem("SL45i/6688i", splashImage, 3, (String)null);
  57.       this.splashScreen.append(it);
  58.       this.menuCommand = new Command(CoctailDatabase.getString(2), 1, 1);
  59.       this.quitCommand = new Command(CoctailDatabase.getString(1), 2, 2);
  60.       this.aboutCommand = new Command(CoctailDatabase.getString(21), 1, 3);
  61.       this.helpCommand = new Command(CoctailDatabase.getString(22), 1, 4);
  62.       this.splashScreen.addCommand(this.quitCommand);
  63.       this.splashScreen.addCommand(this.menuCommand);
  64.       this.splashScreen.addCommand(this.aboutCommand);
  65.       this.splashScreen.addCommand(this.helpCommand);
  66.       this.splashScreen.setCommandListener(this);
  67.       this.display.setCurrent(this.splashScreen);
  68.    }
  69.  
  70.    private void showAbout() {
  71.       Alert about = new Alert(CoctailDatabase.getString(21));
  72.       about.setString("version 1.11\n\n(c)2001 Cocoasoft\nwww.cocoasoft.com");
  73.       this.display.setCurrent(about, this.splashScreen);
  74.    }
  75.  
  76.    private void showHelp() {
  77.       this.helpScreen = new Form(CoctailDatabase.getString(22));
  78.       this.helpScreen.append(CoctailDatabase.getString(23));
  79.       this.backCommand = new Command(CoctailDatabase.getString(4), 2, 1);
  80.       this.helpScreen.addCommand(this.backCommand);
  81.       this.helpScreen.setCommandListener(this);
  82.       this.display.setCurrent(this.helpScreen);
  83.    }
  84.  
  85.    private void buildListUI() {
  86.       String[] list = new String[]{CoctailDatabase.getString(12), CoctailDatabase.getString(14), CoctailDatabase.getString(15), CoctailDatabase.getString(16)};
  87.       this.menuScreen = new List(CoctailDatabase.getString(0), 3, list, (Image[])null);
  88.       this.exitCommand = new Command(CoctailDatabase.getString(1), 7, 1);
  89.       this.menuScreen.addCommand(this.exitCommand);
  90.       this.menuScreen.setCommandListener(this);
  91.    }
  92.  
  93.    private void showCoctail(Coctail coctail) {
  94.       this.coctailScreen = new Form(coctail.getName());
  95.       this.coctailScreen.append(coctail.createCoctailDisplay());
  96.       int score = coctail.getScore();
  97.       if (score > 0) {
  98.          this.coctailScreen.append(CoctailDatabase.getString(17) + ": " + score);
  99.       }
  100.  
  101.       this.backCommand = new Command(CoctailDatabase.getString(4), 2, 1);
  102.       this.coctailScreen.addCommand(this.backCommand);
  103.       this.sendSMSCommand = new Command(CoctailDatabase.getString(19), 1, 2);
  104.       this.coctailScreen.addCommand(this.sendSMSCommand);
  105.       if (score == 0) {
  106.          this.score1Command = new Command(CoctailDatabase.getString(3) + " - 1", 1, 3);
  107.          this.score2Command = new Command(CoctailDatabase.getString(3) + " - 2", 1, 4);
  108.          this.score3Command = new Command(CoctailDatabase.getString(3) + " - 3", 1, 5);
  109.          this.score4Command = new Command(CoctailDatabase.getString(3) + " - 4", 1, 6);
  110.          this.score5Command = new Command(CoctailDatabase.getString(3) + " - 5", 1, 7);
  111.          this.coctailScreen.addCommand(this.score1Command);
  112.          this.coctailScreen.addCommand(this.score2Command);
  113.          this.coctailScreen.addCommand(this.score3Command);
  114.          this.coctailScreen.addCommand(this.score4Command);
  115.          this.coctailScreen.addCommand(this.score5Command);
  116.       }
  117.  
  118.       this.coctailScreen.setCommandListener(this);
  119.       this.display.setCurrent(this.coctailScreen);
  120.    }
  121.  
  122.    private void browseCoctails(String[] coctailList) {
  123.       this.browseScreen = new List(CoctailDatabase.getString(12), 3, coctailList, (Image[])null);
  124.       this.menuCommand = new Command(CoctailDatabase.getString(2), 2, 1);
  125.       this.browseScreen.addCommand(this.menuCommand);
  126.       this.browseScreen.setCommandListener(this);
  127.       this.display.setCurrent(this.browseScreen);
  128.    }
  129.  
  130.    private void searchCoctail() {
  131.       this.searchScreen = new Form(CoctailDatabase.getString(14));
  132.       this.searchByNameField = new TextField(CoctailDatabase.getString(7), "", 30, 0);
  133.       this.searchByIngredientsField = new TextField(CoctailDatabase.getString(8), "", 30, 0);
  134.       this.searchScreen.append(this.searchByNameField);
  135.       this.searchScreen.append(this.searchByIngredientsField);
  136.       this.menuCommand = new Command(CoctailDatabase.getString(2), 2, 1);
  137.       this.searchScreen.addCommand(this.menuCommand);
  138.       this.searchCommand = new Command(CoctailDatabase.getString(5), 1, 2);
  139.       this.searchScreen.addCommand(this.searchCommand);
  140.       this.searchScreen.setCommandListener(this);
  141.       this.display.setCurrent(this.searchScreen);
  142.    }
  143.  
  144.    private void showRandomCoctail() {
  145.       Coctail coctail = this.coctailBar.getRandomCoctail();
  146.       this.showCoctail(coctail);
  147.    }
  148.  
  149.    private void createNewCoctail() {
  150.       this.newCoctailScreen = new Form(CoctailDatabase.getString(18));
  151.       this.coctailNameField = new TextField(CoctailDatabase.getString(9) + ":", "", 30, 0);
  152.       this.coctailIngredientsField = new TextField(CoctailDatabase.getString(10) + ":", "", 60, 0);
  153.       this.coctailRecipeField = new TextField(CoctailDatabase.getString(11) + ":", "", 200, 0);
  154.       this.newCoctailScreen.append(this.coctailNameField);
  155.       this.newCoctailScreen.append(this.coctailIngredientsField);
  156.       this.newCoctailScreen.append(this.coctailRecipeField);
  157.       this.menuCommand = new Command(CoctailDatabase.getString(2), 2, 1);
  158.       this.newCoctailScreen.addCommand(this.menuCommand);
  159.       this.newCommand = new Command(CoctailDatabase.getString(6), 1, 2);
  160.       this.newCoctailScreen.addCommand(this.newCommand);
  161.       this.newCoctailScreen.setCommandListener(this);
  162.       this.display.setCurrent(this.newCoctailScreen);
  163.    }
  164.  
  165.    private void selectSMSNumber(String name) {
  166.       this.SMSScreen = new Form(name);
  167.       this.phoneNumberField = new TextField(CoctailDatabase.getString(20), "", 20, 2);
  168.       this.SMSScreen.append(this.phoneNumberField);
  169.       this.backCommand = new Command(CoctailDatabase.getString(4), 2, 1);
  170.       this.SMSScreen.addCommand(this.backCommand);
  171.       this.sendSMSCommand = new Command(CoctailDatabase.getString(19), 1, 2);
  172.       this.SMSScreen.addCommand(this.sendSMSCommand);
  173.       this.SMSScreen.setCommandListener(this);
  174.       this.display.setCurrent(this.SMSScreen);
  175.    }
  176.  
  177.    private void SendCoctailSMS(String number, String name) {
  178.       Coctail coctail = this.coctailBar.getCoctail(name);
  179.       String coctailSMS = coctail.createCoctailSMS();
  180.  
  181.       try {
  182.          new SMS();
  183.          if (coctailSMS.length() > 160) {
  184.             int startIndex = 0;
  185.             int stopIndex = 159;
  186.  
  187.             while(startIndex < coctailSMS.length()) {
  188.                SMS.send(number, coctailSMS.substring(startIndex, stopIndex));
  189.                startIndex = stopIndex;
  190.                stopIndex += 159;
  191.                if (stopIndex > coctailSMS.length()) {
  192.                   stopIndex = coctailSMS.length();
  193.                }
  194.             }
  195.          } else {
  196.             SMS.send(number, coctailSMS);
  197.          }
  198.       } catch (Exception var8) {
  199.       }
  200.  
  201.       this.showCoctail(coctail);
  202.    }
  203.  
  204.    protected void startApp() {
  205.       this.coctailBar = new CoctailBar();
  206.       CoctailDatabase.loadCoctails(this.coctailBar);
  207.       this.showSplash();
  208.       this.buildListUI();
  209.       this.display.setCurrent(this.splashScreen);
  210.    }
  211.  
  212.    protected void pauseApp() {
  213.    }
  214.  
  215.    protected void destroyApp(boolean b) {
  216.    }
  217.  
  218.    public void exit() {
  219.       this.destroyApp(false);
  220.       ((MIDlet)this).notifyDestroyed();
  221.    }
  222.  
  223.    public void commandAction(Command c, Displayable d) {
  224.       if (c == this.exitCommand || c == this.quitCommand) {
  225.          this.exit();
  226.       }
  227.  
  228.       if (d == this.splashScreen) {
  229.          if (c == this.menuCommand) {
  230.             this.display.setCurrent(this.menuScreen);
  231.          } else if (c == this.aboutCommand) {
  232.             this.showAbout();
  233.          } else if (c == this.helpCommand) {
  234.             this.showHelp();
  235.          }
  236.       } else if (d == this.menuScreen) {
  237.          if (c == List.SELECT_COMMAND) {
  238.             int choice = this.menuScreen.getSelectedIndex();
  239.             switch (choice) {
  240.                case 0:
  241.                   this.browseCoctails(this.coctailBar.getCoctailList());
  242.                   break;
  243.                case 1:
  244.                   this.searchCoctail();
  245.                   break;
  246.                case 2:
  247.                   this.showRandomCoctail();
  248.                   break;
  249.                case 3:
  250.                   this.createNewCoctail();
  251.             }
  252.          }
  253.       } else if (d == this.browseScreen) {
  254.          if (c == this.menuCommand) {
  255.             this.display.setCurrent(this.menuScreen);
  256.          } else {
  257.             String choice = this.browseScreen.getString(this.browseScreen.getSelectedIndex());
  258.             this.showCoctail(this.coctailBar.getCoctail(choice));
  259.          }
  260.       } else if (d == this.coctailScreen) {
  261.          if (c == this.backCommand) {
  262.             this.display.setCurrent(this.menuScreen);
  263.          } else if (c == this.sendSMSCommand) {
  264.             this.selectSMSNumber(this.coctailScreen.getTitle());
  265.          } else if (c == this.score1Command) {
  266.             CoctailDatabase.storeScore(this.coctailBar.setScore(this.coctailScreen.getTitle(), 1));
  267.             this.coctailScreen.removeCommand(this.score1Command);
  268.             this.coctailScreen.removeCommand(this.score2Command);
  269.             this.coctailScreen.removeCommand(this.score3Command);
  270.             this.coctailScreen.removeCommand(this.score4Command);
  271.             this.coctailScreen.removeCommand(this.score5Command);
  272.             this.coctailScreen.append(CoctailDatabase.getString(17) + ": " + 1);
  273.          } else if (c == this.score2Command) {
  274.             CoctailDatabase.storeScore(this.coctailBar.setScore(this.coctailScreen.getTitle(), 2));
  275.             this.coctailScreen.removeCommand(this.score1Command);
  276.             this.coctailScreen.removeCommand(this.score2Command);
  277.             this.coctailScreen.removeCommand(this.score3Command);
  278.             this.coctailScreen.removeCommand(this.score4Command);
  279.             this.coctailScreen.removeCommand(this.score5Command);
  280.             this.coctailScreen.append(CoctailDatabase.getString(17) + ": " + 2);
  281.          } else if (c == this.score3Command) {
  282.             CoctailDatabase.storeScore(this.coctailBar.setScore(this.coctailScreen.getTitle(), 3));
  283.             this.coctailScreen.removeCommand(this.score1Command);
  284.             this.coctailScreen.removeCommand(this.score2Command);
  285.             this.coctailScreen.removeCommand(this.score3Command);
  286.             this.coctailScreen.removeCommand(this.score4Command);
  287.             this.coctailScreen.removeCommand(this.score5Command);
  288.             this.coctailScreen.append(CoctailDatabase.getString(17) + ": " + 3);
  289.          } else if (c == this.score4Command) {
  290.             CoctailDatabase.storeScore(this.coctailBar.setScore(this.coctailScreen.getTitle(), 4));
  291.             this.coctailScreen.removeCommand(this.score1Command);
  292.             this.coctailScreen.removeCommand(this.score2Command);
  293.             this.coctailScreen.removeCommand(this.score3Command);
  294.             this.coctailScreen.removeCommand(this.score4Command);
  295.             this.coctailScreen.removeCommand(this.score5Command);
  296.             this.coctailScreen.append(CoctailDatabase.getString(17) + ": " + 4);
  297.          } else if (c == this.score5Command) {
  298.             CoctailDatabase.storeScore(this.coctailBar.setScore(this.coctailScreen.getTitle(), 5));
  299.             this.coctailScreen.removeCommand(this.score1Command);
  300.             this.coctailScreen.removeCommand(this.score2Command);
  301.             this.coctailScreen.removeCommand(this.score3Command);
  302.             this.coctailScreen.removeCommand(this.score4Command);
  303.             this.coctailScreen.removeCommand(this.score5Command);
  304.             this.coctailScreen.append(CoctailDatabase.getString(17) + ": " + 5);
  305.          }
  306.       } else if (d == this.newCoctailScreen) {
  307.          if (c == this.newCommand) {
  308.             Coctail coctail = new Coctail(this.coctailNameField.getString(), this.coctailIngredientsField.getString(), this.coctailRecipeField.getString());
  309.             this.coctailBar.addCoctail(coctail);
  310.             CoctailDatabase.storeCoctail(coctail);
  311.             this.display.setCurrent(this.menuScreen);
  312.          } else if (c == this.menuCommand) {
  313.             this.display.setCurrent(this.menuScreen);
  314.          }
  315.       } else if (d == this.searchScreen) {
  316.          if (c == this.menuCommand) {
  317.             this.display.setCurrent(this.menuScreen);
  318.          } else if (c == this.searchCommand) {
  319.             String searchByName = this.searchByNameField.getString();
  320.             if (searchByName == null) {
  321.                searchByName = new String("");
  322.             }
  323.  
  324.             String searchByIngredients = this.searchByIngredientsField.getString();
  325.             if (searchByIngredients == null) {
  326.                searchByIngredients = new String("");
  327.             }
  328.  
  329.             String[] coctailList = this.coctailBar.searchCoctail(searchByName, searchByIngredients);
  330.             if (coctailList != null) {
  331.                this.browseCoctails(coctailList);
  332.             } else {
  333.                this.display.setCurrent(this.searchScreen);
  334.             }
  335.          }
  336.       } else if (d == this.SMSScreen) {
  337.          if (c == this.backCommand) {
  338.             this.showCoctail(this.coctailBar.getCoctail(this.SMSScreen.getTitle()));
  339.          } else if (c == this.sendSMSCommand) {
  340.             this.SendCoctailSMS(this.phoneNumberField.getString(), this.SMSScreen.getTitle());
  341.          }
  342.       } else if (d == this.helpScreen && c == this.backCommand) {
  343.          this.showSplash();
  344.       }
  345.  
  346.    }
  347. }
  348.